package com.future.dao;

import java.sql.Connection;
import java.util.List;
import java.util.Map;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import com.future.model.po.InforShareAnswerModel;
import com.future.model.po.UserModel;

public class InforShareAnswerDAO extends BaseDAO<InforShareAnswerModel> {
	private static Logger logger = LogManager.getLogger(InforShareAnswerDAO.class);

	public InforShareAnswerDAO() {

	}

	public InforShareAnswerDAO(Connection conn) {
		super(conn);
	}

	public boolean save(InforShareAnswerModel model) {
		String sql = "insert into infor_share_answer (userid,inforid,isShare,shareType) values (?,?,?,?)";
		logger.info("sql=" + sql);
		return this.update(sql, model.getUserid(), model.getInforid(), model.getIsShare(), model.getShareType().replaceAll("'", ""));
	}

	public boolean update(InforShareAnswerModel model) {
		String sql = "update infor_share_answer set isShare=?,shareType=? where userid=? and inforid=?";
		logger.info("sql=" + sql);
		return this.update(sql, model.getIsShare(), model.getShareType().replaceAll("'", ""), model.getUserid(), model.getInforid());
	}

	public List<InforShareAnswerModel> findByUserId(Integer userId) throws Exception {
		InforShareAnswerModel model = new InforShareAnswerModel();
		model.setUserid(userId);
		return this.findByUserId(model);
	}

	public List<InforShareAnswerModel> findByUserId(InforShareAnswerModel model) throws Exception {
		String sql = "select * from infor_share_answer where userid = ? ";
		List<InforShareAnswerModel> modelList = this.searchAsBeanList(sql, InforShareAnswerModel.class, model.getUserid());
		if (modelList.size() < 1) {
			throw new Exception("该用户未答题");
		}
		return modelList;
	}

	/**
	 * 判断当前登录用户是否答过该调查
	 * 
	 * @param model
	 * @return true 表示答过 false 表示没答过
	 */
	public boolean isOrExist(UserModel model) {
		String sql = "select * from infor_share_answer where userid = ? ";
		List<InforShareAnswerModel> list = this.searchAsBeanList(sql, InforShareAnswerModel.class, model.getId());
		if (list != null && list.size() > 0) {
			return true;
		} else {
			return false;
		}
	}

	public List<Map<String, Object>> count(Integer areaid) {
		String sql = "select i.id inforid, sd.id deptid , sd.department department, i.information information, sum(case when isa.isshare = -1 then 0 else isa.isshare end )  shareTotle from sharedepartment sd join information i on sd.id = i.deptid left join (select * from infor_share_answer where userid in ( select id from user where areaid = ? ) ) as isa on isa.inforid = i.id group by i.id order by sd.id asc , i.id asc";
		logger.debug(sql);
		return this.searchAsMapList(sql, areaid);
	}

	public List<Map<String, Object>> countShareType(Integer areaId) {
		String sql = "select sum(A) as A , sum(B) as B , sum(C) as C from (select userid, inforid , case when find_in_set('A',shareType) > 0 then 1 else 0 end as A  , case when find_in_set('B' , shareType) > 0 then 1 else 0 end as B , case when find_in_set('C',shareType) > 0 then 1 else 0 end as C from infor_share_answer ) as infor_share_answer where userid in (select id from user where areaid = ? )";
		logger.debug(sql);
		return this.searchAsMapList(sql, areaId);
	}
}
